package com.arthur.controller;

import com.arthur.entity.Dept;
import com.arthur.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    /**
     * 添加部门
     */
    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public boolean addDept(@RequestBody Dept dept) {
        return deptService.addDept(dept);
    }
    /**
     * 根据id获取部门
     */
    @RequestMapping(value = "/dept/find/{id}", method = RequestMethod.GET)
    public Dept findById(@PathVariable("id") Long id) {
        return deptService.findById(id);
    }

    /**
     * 获取部门列表
     */
    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public List<Dept> findAll() {
        return deptService.findAll();
    }

    // 增加eureka的服务发现
     @Autowired
     private DiscoveryClient discoveryClient;
     @RequestMapping(value = "dept/discovery",method = RequestMethod.GET)
     public Object discovery(){
         List<String> stringList=discoveryClient.getServices();
         System.out.println(">>>>>>>>>>>"+stringList);
         List<ServiceInstance> serviceInstanceList=discoveryClient.getInstances("microservicecloud_dept");
         for (ServiceInstance serviceInstance : serviceInstanceList) {
             System.out.println(serviceInstance.getServiceId()+"--"+serviceInstance.getHost()+"--"+serviceInstance.getPort()+"--"+serviceInstance.getUri());
         }
         return this.discoveryClient;
     }
}
